package easy;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/26 14:24
 */
public class RomanToInteger {
    public static void main(String[] args) {
        Solution3 solution = new RomanToInteger().new Solution3();
        String s = "IV";
        System.out.println(solution.romanToInt(s));
    }

    private class Solution {
        public int romanToInt(String s) {
            Map<Character, Integer> map = new HashMap<>();
            map.put('I', 1);
            map.put('V', 5);
            map.put('X', 10);
            map.put('L', 50);
            map.put('C', 100);
            map.put('D', 500);
            map.put('M', 1000);

            char[] chars = s.toCharArray();
            int result = 0;

            for (int i = 0; i < chars.length; i++) {
                if (('I' == chars[i])) {
                    if (i == chars.length - 1) {
                        result += 1;
                    } else {
                        if ('V' == chars[i + 1]) {
                            i += 1;
                            result += 4;
                        } else if ('X' == chars[i + 1]) {
                            i += 1;
                            result += 9;
                        } else {
                            result += 1;
                        }
                    }

                } else if ('X' == chars[i]) {
                    if (i == chars.length - 1) {
                        result += 10;
                    } else {
                        if ('L' == chars[i + 1]) {
                            i += 1;
                            result += 40;
                        } else if ('C' == chars[i + 1]) {
                            i += 1;
                            result += 90;
                        } else {
                            result += 10;
                        }
                    }
                } else if ('C' == chars[i]) {
                    if (i == chars.length - 1) {
                        result += 100;
                    } else {
                        if ('D' == chars[i + 1]) {
                            i += 1;
                            result += 400;
                        } else if ('M' == chars[i + 1]) {
                            i += 1;
                            result += 900;
                        } else {
                            result += 100;
                        }
                    }
                } else {
                    result += map.get(chars[i]);
                }
            }

            return result;
        }
    }

    private class Solution2 {
        public int romanToInt(String s) {
            Map<Character, Integer> map = new HashMap<>();
            map.put('I', 1);
            map.put('V', 5);
            map.put('X', 10);
            map.put('L', 50);
            map.put('C', 100);
            map.put('D', 500);
            map.put('M', 1000);

            Map<Character, Character> Cmap = new HashMap<>();

            int result = 0;

            char[] chars = s.toCharArray();
            for (int i = chars.length - 1; i >= 0; i--) {
                char one = chars[i];
                char two = i == 0 ? 'n' : chars[i - 1];
                if (one == 'X' || one == 'V') {
                    if (two == 'I') {
                        result += (map.get(one) - 1);
                        i -= 1;
                        continue;
                    }
                } else if (one == 'L' || one == 'C') {
                    if (two == 'X') {
                        result += (map.get(one) - 10);
                        i -= 1;
                        continue;
                    }
                } else if (one == 'D' || one == 'M') {
                    if (two == 'C') {
                        result += (map.get(one) - 100);
                        i -= 1;
                        continue;
                    }
                }
                result += map.get(one);

            }

            return result;
        }
    }

    private class Solution3 {
        public int romanToInt(String s) {
            Map<Character, Integer> map = new HashMap<>();
            map.put('I', 1);
            map.put('V', 5);
            map.put('X', 10);
            map.put('L', 50);
            map.put('C', 100);
            map.put('D', 500);
            map.put('M', 1000);

            Map<Character, Character> Cmap = new HashMap<>();

            int result = 0;

            char[] chars = s.toCharArray();
            for (int i = chars.length - 1; i >= 0; i--) {
                char one = chars[i];
                char two = i == 0 ? 'n' : chars[i - 1];
                switch (one) {
                    case 'X':
                    case 'V':
                        if (two == 'I') {
                            result += (map.get(one) - 1);
                            i -= 1;
                        } else {
                            result += map.get(one);
                        }
                        break;
                    case 'L':
                    case 'C':
                        if (two == 'X') {
                            result += (map.get(one) - 10);
                            i -= 1;
                        } else {
                            result += map.get(one);
                        }
                        break;
                    case 'D':
                    case 'M':
                        if (two == 'C') {
                            result += (map.get(one) - 100);
                            i -= 1;
                        } else {
                            result += map.get(one);
                        }
                        break;
                    default:
                        result += map.get(one);
                }
            }

            return result;
        }
    }
}
